package com.csx.ops.service.node;

import com.csx.ops.core.XStreamFactory;
import com.csx.ops.core.XmlUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.var;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 状态机
 */
@Data
@XStreamAlias("StateXml")
public class StateEngine {

    @XStreamAlias("Nodes")
    private List<BaseNode> nodes;

    public void flow(){

    }

    public StateEngine fromXml(String xml){
//        for(var kv:getMapClass()){
//           xml = xml.replace(kv.getSimpleName(),kv.getName());
//        }
        XStream xStream = new XStream(new DomDriver("utf-8"));
        xStream.processAnnotations(this.getClass());
        for(var kv:getMapClass()){
            xStream.processAnnotations(kv);
        }
        return (StateEngine) xStream.fromXML(xml);
    }

    private List<Class> getMapClass(){
        var mapClass = new ArrayList<Class>();
        mapClass.add(StartNode.class);
        mapClass.add(ApolloNode.class);
        mapClass.add(JenkinsNode.class);
        mapClass.add(NotifyNode.class);
        mapClass.add(ShellNode.class);
        mapClass.add(SqlNode.class);
        mapClass.add(EndNode.class);
        return mapClass;
    }
    public String toXml(){
        XStream xStream = XStreamFactory.getXStream(new XmlFriendlyNameCoder("_-","_"));
        xStream.processAnnotations(this.getClass());
        for(var kv:getMapClass()){
            xStream.processAnnotations(kv);
        }
        var xml = xStream.toXML(this);
        return xml;
    }

    public void fromJson(){

    }

    public String toJson(){
        return "";
    }
}
